package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.pojo.dto.shoppingCartDto.ShoppingCartAddDto;
import com.itheima.pojo.dto.shoppingCartDto.ShoppingCartDeleteDto;
import com.itheima.pojo.vo.shoppingCartVo.GetShoppingCartVo;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService service;

    @PostMapping("/add")
    public R add(@RequestBody ShoppingCartAddDto cart){
        boolean add = service.add(cart);
        if (add){
            return  R.success(cart);
        }
        return R.error("添加失败");
    }
    @DeleteMapping("/clean")
    public R clean(){
        boolean clean= service.clean();
        if (clean){
            return  R.success("清空购物车成功");
        }
        return  R.error("清空失败");
    }
    @GetMapping("/list")
    public R selectAll(){
        List<GetShoppingCartVo>list =service.selectAll();
        return R.success(list);
    }
    @PostMapping("/sub")
    public R delete(@RequestBody ShoppingCartDeleteDto shoppingCart){
        boolean result = service.deleteByDishId( shoppingCart);
        if (result){
            return R.success("删除成功");
        }

        return R.error("删除失败");
    }
}
